package com.idbk.chargestation.activity.charge;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.idbk.chargestation.R;
import com.idbk.chargestation.api.ChargeStationAPI;
import com.idbk.chargestation.base.BaseActivity;
import com.idbk.chargestation.bean.JsonCommentResult;
import com.idbk.chargestation.net.EHttpResponse;
import com.idbk.chargestation.util.Const;
import com.idbk.chargestation.util.GsonUtils;

/**
 * 评价界面
 * @author lupc
 *
 */
public class ActivityComment extends BaseActivity implements OnClickListener,
OnRatingBarChangeListener {

	public final static String TAG = ActivityComment.class.getSimpleName();

	private Context mContext;
	
	private EditText etComment;
	private RatingBar mRatingBar;
	private TextView mTextView;
	private int mGrade = 0, mPointId;//评分, 充电点Id

	@Override
	protected void onCreate(Bundle savedInstanceState) {		
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_comment);

		setupView();

		setupData();

		mContext = this;
	}

	private void setupView() {

		findViewById(R.id.textview_cancel).setOnClickListener(this);
		
		etComment = (EditText)findViewById(R.id.edittext_comment);
		mRatingBar = (RatingBar) findViewById(R.id.ratingbar_comment);
		mTextView = (TextView) findViewById(R.id.textview_comment_grade);

		mRatingBar.setOnRatingBarChangeListener(this);
		findViewById(R.id.textview_ok).setOnClickListener(this);
	}

	private void setupData(){
		//获得intent意图对象
		Intent intent = getIntent();
		mPointId = intent.getExtras().getInt(Const.KEY_ID,0);
	}

	@Override
	public void onClick(View v) {
		if(v.getId() == R.id.textview_ok){
			if(mGrade == 0) {
				Toast.makeText(mContext, "你还未进行打分哦", Toast.LENGTH_SHORT).show();
			} else {
				submitComment();
			}
		} else if(v.getId() == R.id.textview_cancel){
			finish();
		}
	}

	private void submitComment(){
		//1.获取评论内容
		String comment = etComment.getText().toString();

		//2.校验
		if(comment.equals("") || comment.length() < 10 || comment.length() > 300 ){
			Toast.makeText(
					mContext, 
					R.string.activity_comment_tip_comment_length,
					Toast.LENGTH_SHORT)
					.show();
			return;
		}
		//3.提交
		showMyProgressDialog(true, false, "提交中");
		mMainRequest = ChargeStationAPI.submitPointComment(
				mPointId, 
				comment, 
				mGrade, 
				mResponse);
	}
	
	private final EHttpResponse mResponse = new EHttpResponse() {
		
		@Override
		public void onEResponse(String response) {
			final JsonCommentResult result = GsonUtils.toBean(JsonCommentResult.class, response);
			if(handleResponseStatus(result)) {
				Toast.makeText(
						mContext, 
						"评论成功", 
						Toast.LENGTH_SHORT)
						.show();
				Intent data = new Intent();
				result.info.mark = mGrade;
				result.info.comment = etComment.getText().toString();
				data.putExtra(JsonCommentResult.JsonCommentResultInfo.KEY, result.info);
				ActivityComment.this.setResult(RESULT_OK,data);
				ActivityComment.this.finish();
			}
		}

		@Override
		public void onEErrorResponse(VolleyError error) {
			Toast.makeText(
					mContext, 
					R.string.common_tip_resultnull,
					Toast.LENGTH_SHORT)
					.show();
		}

		@Override
		public void onEFinish() {
			super.onEFinish();
			dismissMyProgressDialog();
		}
		
	};	

	@Override
	public void onRatingChanged(RatingBar ratingBar, float rating,
			boolean fromUser) {
		mTextView.setText("(" + rating + "分)");
		mGrade = (int) rating;
	}

}
